/* 
 * File:   photonMap.cpp
 * Author: flo
 * 
 * Created on July 9, 2010, 1:47 PM
 */

#include "photonMap.h"

photonMap::photonMap(Scene* scene) : Render(scene) {
    init();
}

photonMap::~photonMap() {
}

bool photonMap::init() {
    //while (m_vPMap.size() < PMAP_MAX_SIZE)
        genPhoton();

    return true;
}

void photonMap::genPhoton() {
    int id;
    Vec3 light_point = scene->sampleRandomLight(id, mtrand->randExc(), mtrand->randExc(), mtrand->randExc());
    Vec3 dir(0.0);
    //cout<<id<<endl;
    //dir = sample_hemisphere(scene->triangles[id].getNormal());
    dir.normalize();
    Ray ray(light_point, dir, RAY_EPS, RAY_MAX);
    HitRec rec = accel->intersect(ray);
    //light_trace(ray, rec, scene->material[scene->mat_index[id]].color_e * scene->num_lights * scene->triangles[id].area());
    nr_samples++;
}

photonMap::PMHeap photonMap::getNearestPhotons_linear(PMap* pmap, Vec3 reference, int num_photons) {
    PMapCIt it = pmap->begin();
    PMHeap heap(pmap, reference, num_photons);
    while(it<pmap->end()){
        heap.add(*it);
    }
    return heap;
}